#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>

int main()
{
	int ADCval;
	ADMUX = (1<<REFS0)|(1<<ADLAR);	//5V reference and left justified
	ADCSRA = (1<<ADEN);				//Enable ADC
	ADCSRB = 0x00;					//Free running mode
	DDRB = 0xFF;					//make PORTB output
	
	//Configure Timer
	//ICR1 = 20000 defines 50Hz pwm
	ICR1 = 20000;
	TCCR1A |= (1<<COM1A1)|(1<<WGM11);
	TCCR1B |= (1<<WGM13)|(1<<WGM12)|(1<<CS11);
	
	while(1)
	{
		ADMUX = (ADMUX & 0xF0)|(0 & 0x0F);	//ADC0
		ADCSRA |= 1<<ADSC;					//Convert
		while(ADCSRA & (1<<ADSC));			//Wait until finished
		ADCval = ADC>>6;					//Shift right 6 to use converted value
		OCR1A = (int)ADCval*1.66+500;		//Convert ADC value to stepper position
		
		_delay_ms(100);
	}
	return 0 ;
}